//
//  FJC_HotListViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FJC_HotListViewController.h"
#import "FJC_SwipeableView.h"
#import "FJC_AVswipeCardCell.h"
#import "AGLChannel.h"
#import "FJC_VideoView.h"
#import "VideoDetailViewController.h"
#import "Header.h"

@interface FJC_HotListViewController ()<FJC_SwipeableViewDataSource, FJC_SwipeableViewDelegate>

@property (nonatomic, strong) NSMutableArray *cardInfoList;
@property (nonatomic, strong) FJC_SwipeableView *swipeableView;
@property (nonatomic, strong) ZFPlayerView *playerView;

@end

@implementation FJC_HotListViewController

- (NSMutableArray *)cardInfoList {
    if (!_cardInfoList) {
        _cardInfoList = [NSMutableArray array];
    }
    return _cardInfoList;
}

- (FJC_SwipeableView *)swipeableView {
    
    if (!_swipeableView) {
        _swipeableView = [[FJC_SwipeableView alloc] initWithFrame:self.view.bounds];
        _swipeableView.datasource = self;
        _swipeableView.delegate = self;
        _swipeableView.backgroundColor = [UIColor whiteColor];
        _swipeableView.topCardInset = UIEdgeInsetsMake(20, 20, 20, 20);
        _swipeableView.hidden = YES;
    }
    return _swipeableView;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"热榜";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.swipeableView];
    self.swipeableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.swipeableView registerClass:[FJC_AVswipeCardCell class] forCellReuseIdentifier:@"reuseCell"];
    
    for (int i = 0; i < 50; i++) {
        AGLChannel *info = [[AGLChannel alloc] init];
        [self.cardInfoList addObject:info];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.swipeableView.hidden = NO;
        [self.swipeableView reloadData];
    });
    
    [self createData];
    
//    NSLog(@"%f", self.swipeableView.frame.size.width);
    
}

- (void)createData {
    
    NSString *urlString = [NSString stringWithFormat:@"http://api.miaopai.com/m/ascent_channel.json?timestamp=1463473753418&unique_id=0156a9d2-fb05-3001-83e2-e2592b043531&os=android&per=50&token=&page=1&vend=miaopai&udid=0156a9d2-fb05-3001-83e2-e2592b043531&uuid=0156a9d2-fb05-3001-83e2-e2592b043531&channel=huawei&deviceId=0156a9d2-fb05-3001-83e2-e2592b043531"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *resultArr = dic[@"result"];
        self.cardInfoList = [NSMutableArray array];
        for (NSDictionary *tempDic in resultArr) {
            AGLChannel *channel = [AGLChannel new];
            [channel setValuesForKeysWithDictionary:tempDic];
            [self.cardInfoList addObject:channel];
        }
        [self.swipeableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"首页内容collection联网失败");
        //        NSDictionary *dic = [self getDataWithDataName:@"hotMovieTable"];
        //        if (dic == nil) {
        //            NSLog(@"请连网啊亲");
        //        } else {
        //            NSDictionary *data = dic[@"data"];
        //            NSArray *hot = data[@"hot"];
        //            self.movieArr = [AGLMovie baseModalByArr:hot];
        //            [self.tableView reloadData];
        //        }
        //
        
    }];

    
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.swipeableView.frame = self.view.bounds;
}
// DataSource协议方法
- (NSInteger)swipeableViewNumberOfRowsInSection:(FJC_SwipeableView *)swipeableView{
    return self.cardInfoList.count;
}

- (FJC_SwipeableViewCell *)swipeableView:(FJC_SwipeableView *)swipeableView cellForIndex:(NSInteger)index{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
//    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    FJC_AVswipeCardCell *cell = [[FJC_AVswipeCardCell alloc] init];
    cell.channel = self.cardInfoList[index];
    cell.backgroundColor = [UIColor whiteColor];
    
    __block AGLChannel *channel = self.cardInfoList[indexPath.row];
    cell.channel = channel;
    __block NSIndexPath *weakIndexPath = indexPath;
    __block FJC_AVswipeCardCell *weakCell = cell;
    
    __weak typeof(self) weakSelf = self;
    cell.playBlock = ^(BaseButton *btn) {
#pragma mark 播放器block
        NSURL *videoURL = [NSURL URLWithString:channel.streambase];
        [weakCell.VideoView.playerView setVideoURL:videoURL withView:weakSelf.swipeableView AtIndexPath:weakIndexPath withImageViewTag:1000];
        
        [weakCell.VideoView.playerView addPlayerToCellImageView:weakCell.picImageView];
        weakCell.VideoView.playerView.playerLayerGravity = ZFPlayerLayerGravityResizeAspect;
        
    };
    
    
    return cell;
}

- (NSInteger)swipeableViewMaxCardNumberWillShow:(FJC_SwipeableView *)swipeableView{
    return 4;
}
- (void)swipeableView:(FJC_SwipeableView *)swipeableView didTapCellAtIndex:(NSInteger)index{
    AGLChannel *mod = self.cardInfoList[index];
    VideoDetailViewController *VDVC = [VideoDetailViewController new];
    VDVC.scid = mod.scid;
    VDVC.videoURL  = [NSURL URLWithString:mod.streambase];
    [self.navigationController pushViewController:VDVC animated:YES];
}

// 拉到最后一个
- (void)swipeableViewDidLastCardRemoved:(FJC_SwipeableView *)swipeableView{
        
}

- (void)swipeableView:(FJC_SwipeableView *)swipeableView didCardRemovedAtIndex:(NSInteger)index withDirection:(FJC_SwipeableViewCellSwipeDirection)direction{
    NSLog(@"%zd",direction);
}


- (void)swipeableView:(FJC_SwipeableView *)swipeableView didTopCardShow:(FJC_SwipeableViewCell *)topCell{
    
}


- (void)swipeableView:(FJC_SwipeableView *)swipeableView didLastCardShow:(FJC_SwipeableViewCell *)cell{
    
}

// Delegate协议方法
- (void)knackBottomToolViewDidCheckDetailBtnClick:(AGLChannel *)idInfo{
    [self.swipeableView removeTopCardViewFromSwipe:FJC_SwipeableViewCellSwipeDirectionLeft];
}


- (void)knackBottomToolViewDidCollectBtnClick:(AGLChannel *)idInfo{
    [self.swipeableView removeTopCardViewFromSwipe:FJC_SwipeableViewCellSwipeDirectionRight];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
